package com.example.hotelsystem.controller;


import code.CodeError;
import com.example.hotelsystem.entity.Hotelorder;
import com.example.hotelsystem.entity.Room;
import com.example.hotelsystem.entity.TComment;
import com.example.hotelsystem.entity.User;
import com.example.hotelsystem.result.Result;
import com.example.hotelsystem.service.HotelorderService;
import com.example.hotelsystem.service.RoomService;
import com.example.hotelsystem.service.RoomTypeService;
import com.example.hotelsystem.service.TCommentService;
import com.example.hotelsystem.utils.UserUtil;
import com.example.hotelsystem.vo.comment.CommentAddVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import javax.xml.stream.events.Comment;
import java.util.Date;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Lee
 * @since 2021-03-30
 */
@RestController
@RequestMapping("/hotelsystem/comment")
@Api(tags = "评论控制器")
public class TCommentController {

    @Autowired
    private TCommentService tCommentService;
    @Autowired
    private HotelorderService hotelorderService;
    @Autowired
    private RoomService roomService;
    @Autowired
    private RoomTypeService roomTypeService;

    @PostMapping("/add")
    @ApiOperation("对对应的房间类型发表评论")
    public Result toComment(CommentAddVO vo){
        try {
            Hotelorder order = hotelorderService.getById(vo.getOrderId());
            if(order.getStatus()!=3){
                return Result.error("订单当前状态下不可评价");
            }
            Room room = roomService.getById(order.getRoomId());
            TComment tComment = new TComment();
            BeanUtils.copyProperties(vo,tComment);
            User user = UserUtil.getUser();
            if(user==null){
                return Result.error(CodeError.NOT_LOGIN,"请先登录",null);
            }
            Date date = new Date();
            tComment.setRoomTypeId(room.getRoomtypeId());
            tComment.setUserId(user.getId());
            tComment.setCreateTime(date);
            tCommentService.save(tComment);

            order.setStatus(2);
            hotelorderService.updateById(order);
            return Result.success("评论成功");
        }catch (Exception e){
            return Result.error(e.getMessage());
        }
    }
}

